/*
 * huirong Inc.
 * Copyright (c) 2019 All Rights Reserved.
 * Author     :liyb
 * Create Date:2019年3月29日
 */
package cn.zs.supply.common.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;

/**
 * Execl处理工具类
 */
public class ExeclUtil {
    public static final String EXCEL2003 = "xls";
    public static final String EXCEL2007 = "xlsx";
    public static final String RULE2003 = "^.+\\.(?i)(xls)$";
    public static final String RULE2007 = "^.+\\.(?i)(xlsx)$";

    public static String getCellValue(Cell cell,boolean treatAsStr) {
    	try {
        if (cell == null) {
            return "";
        }
        if (treatAsStr) {
            // 虽然excel中设置的都是文本，但是数字文本还被读错，如“1”取成“1.0”
            // 加上下面这句，临时把它当做文本来读取
            cell.setCellType(Cell.CELL_TYPE_STRING);
        }
        if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
            return String.valueOf(cell.getBooleanCellValue()).trim();
        } 
        //自定义时间格式
        else if(cell.getCellStyle().getDataFormat() == 14||
        		cell.getCellStyle().getDataFormat() == 31||
        		cell.getCellStyle().getDataFormat() == 57) {
            String value = cell.getStringCellValue();  
            if(value!=null&&!value.equals("")) {
        	Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(Double.parseDouble(value)); 
        	DateFormat formater = new SimpleDateFormat("yyyy/MM/dd");
        	String dt=formater.format(date);
        	return dt.trim();
            }else {
            return null;	
            }
           
        }
        else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            return String.valueOf(cell.getNumericCellValue()).trim();

        } else {
            return String.valueOf(cell.getStringCellValue()).trim();
        }
    	 }
        catch (Exception e) {
        	e.printStackTrace();
        	return null;
		}
    }
}
